package com.cmcc.linlaker.ui.upload.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cmcc.linlaker.R;
import com.cmcc.linlaker.application.LinhuApp;
import com.cmcc.linlaker.ui.upload.bean.CheckingTaskBean;

import java.util.List;

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {

    private List<CheckingTaskBean.CheckingTaskData> itemList;
    private Context mContext;

    public TaskAdapter(Context mContext, List<CheckingTaskBean.CheckingTaskData> itemList) {
        this.itemList = itemList;
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.check_task_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        CheckingTaskBean.CheckingTaskData item = itemList.get(position);

        holder.text_name.setText(item.name);
        holder.text_type.setText("任务类型：" + getProjectTypeStr(item.type));
        holder.text_death_date.setText("截至时间：" + item.finishTime);


        if (item.project != null) {
            holder.text_project.setText("所属项目：" + item.project.name);
        } else {
            holder.text_project.setText("所属项目：" + "");
        }

        if (item.user != null) {
            holder.text_fzr.setText(item.user.userName);
        } else {
            holder.text_fzr.setText("");
        }

        if (item.status.equals("1")) {
            holder.text_state.setText("未完成");
        } else {
            holder.text_state.setText("已完成");
        }
    }

    private String getProjectTypeStr(String type) {
        if (type.equals("2")) {
            return "指派任务";
        } else if (type.equals("1")) {
            return "核实处置任务";
        } else if (type.equals("3")) {
            return "计划任务";
        } else {
            return "";
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView text_name, text_type, text_project, text_death_date;
        public TextView text_fzr, text_state;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

            text_name = itemView.findViewById(R.id.text_name);
            text_type = itemView.findViewById(R.id.text_type);
            text_project = itemView.findViewById(R.id.text_project);
            text_death_date = itemView.findViewById(R.id.text_death_date);

            text_fzr = itemView.findViewById(R.id.text_fzr);
            text_state = itemView.findViewById(R.id.text_state);
        }

        @Override
        public void onClick(View v) {
            onItemClickListener.onItemClick(getAdapterPosition());
        }
    }

}

